import time
from DataStructure import Table
from Importers.Req import Req
from DataStructure.Course import UploadCourseModel
from DataStructure.Course import ParsedCourse


class Importer:
    def __init__(self, token: str, table_name: str):
        super().__init__()
        self._token = token
        self._table_name = table_name

    def start(self, parsed_courses: dict, timer_data: dict):
        req = Req(self._token)

        # 创建新的课程表
        new_table_id = req.create_table(self._table_name)
        # 获取当前课程表id
        prev_table_id = req.get_current_table()["id"]
        # 切换到新的课程表
        req.switch_table(prev_table_id, new_table_id)
        # 获取当前课程表
        current_table = req.get_current_table()
        # 生成课程表配置数据
        table = Table.CourseTable.from_timer(timer_data, new_table_id, self._table_name, current_table)
        # 修改课程表配置
        req.modify_table(table)
        # 上传课程
        for i in parsed_courses:
            time.sleep(1)
            course = ParsedCourse(i["name"], i["teacher"], i["position"], i["day"], i["weeks"], i["sections"])
            upload_course = UploadCourseModel(course, new_table_id)
            req.create_course(upload_course)
